define(["angularAMD"], function (angularAMD) {
    angularAMD.service('SaveModuleService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            var service = this;

            // model_no : 1(正向追溯)、2(生產追溯)
            service.init = function(model_no, dashboardPanel){
                openDialog(model_no, dashboardPanel);
            };

            function openDialog(model_no, dashboardPanel){
                $mdDialog.dialog(
                    'module/moduleQT/program/service/SaveModule.tmp.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/service/SaveModule.css',
                            model_name: '',
                            back:function(){
                                dialog.hide();
                            },
                            confirm:function(){
                                var content = {
                                    model_no: model_no,
                                    model_name: dialog.model_name,
                                    model_setting: dashboardPanel
                                };
                                saveModuleProcess(content, function(data){
                                    if(data.code == '1'){
                                        $rootScope.showToast($filter('translate')('QT_standard.common.saveModule.msg.save_success'));
                                        dialog.hide();
                                    } else {
                                        $rootScope.showAlert($filter('translate')('QT_standard.common.saveModule.msg.save_fail'));
                                    };
                                });
                                saveTempInfo(content, function (data) {
                                    console.log(data);
                                });
                            }
                        };
                    }
                );
            };

            function saveTempInfo(content, callback) {
                if (callback){
                    callback('11111');
                }
            };

            // 儲存模版服務
            function saveModuleProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.save_template_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

        }
    ])
});
